$(function (){
    $('#keyword').on('keyup',function (){
        //获得按键
        var keyword=$(this).val();
        if(keyword!==null&&keyword!==''&&keyword.length!==0){
            $.ajax({
                type:'GET',
                url:'http://localhost:8080/untitled/productAuto?keyword='+encodeURIComponent(keyword),
                success:function (data){
                    console.log(data);
                    var productListHTML='';
                    for(var i=0;i<data.length;i++)
                    {
                        productListHTML+='<li class=\"productAutoItem\" data-productId="';
                        productListHTML+=data[i].productId;
                        productListHTML+='">';
                        productListHTML+=data[i].categoryId;
                        productListHTML+=': ';
                        productListHTML+=data[i].name;
                        productListHTML+='</li>';
                    }
                    $('#productAutoList').html(productListHTML);
                    $('#productAutoComplete').show();
                },
                error:function (errorMsg){
                    console.log(errorMsg);
                }
            })
        }
        else{
            $('#productAutoComplete').hide();
        }
    });

    /*$('.productAutoItem').on('click',function (){
        console.log(1111);
    })*/

    //动态绑定
    $(document).on('click','.productAutoItem',function (){
        var productId=$(this).data('productid');
        //预处理
        $('#productAutoComplete').hide();
        $('#keyword').val('');
        //跳转对应界面
        window.location.href='http://localhost:8080/untitled/productForm?productId='+encodeURIComponent(productId);
    });

    //鼠标移开效果
    $('#productAutoComplete').on('mouseleave', function () {
        $(this).hide();
        $('#keyword').val('');
    })

});